package bdz.projects.lavender.opsm.ui { 
	
	import bdz.common.display.DisplayLoader;
	import bdz.common.events.GenericEvent;
	import bdz.projects.lavender.opsm.model.FlipCollection;
	import bdz.projects.lavender.opsm.vo.ProductVO;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	/**
	 * ...
	 * @author ...
	 */
	public class ProductDetailedView extends MovieClip {
		private var _bg:Sprite;
		private var _baseW:Number;
		private var _baseH:Number;
		private var _pvo:ProductVO;
		private var _imagecopy:ProductDetailImageCopy;
		private var _shortlist:ProductShortList;
		private var _brandImageList:BrandImageList;
		private var _currentBitmap:BitmapData;
		private var _collection:FlipCollection;
		private var _thumbW:Number;
		private var _thumbH:Number;
		
		function ProductDetailedView(dw:Number = 720, dh:Number = 650) {
			_baseW = dw;
			_baseH = dh;
			visible = false;
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		private function initializeComponent(evt:Event):void {
			_bg = new Sprite();
			_bg.graphics.beginFill(0xffffff, 1);
			_bg.graphics.drawRect(0, 0, _baseW, _baseH);
			_bg.graphics.endFill();
			addChild(_bg);
			createUIElements();
		}
		private function onMainImageReady(evt:GenericEvent):void {
			dispatchEvent(evt);
		}
		private function createUIElements():void {
			_imagecopy = new ProductDetailImageCopy();
			_imagecopy.addEventListener(Event.COMPLETE, onMainImageLoaded);
			_imagecopy.addEventListener(GenericEvent.READY, onMainImageReady);
			_imagecopy.addEventListener(GenericEvent.CLICKED, onColorClicked);
			_imagecopy.product = product;
			_imagecopy.images = product.allImages;
			_imagecopy.collection = _collection;
			_imagecopy.thumbWidth = _thumbW;
			_imagecopy.thumbHeight = _thumbH;
			addChild(_imagecopy);
			
			/*
			 * FOR PHASE 2
			_shortlist = new ProductShortList();
			_shortlist.x = 5;
			_shortlist.y = 450;
			addChild(_shortlist);
			*/
		}
		private function onColorClicked(evt:GenericEvent):void {
			dispatchEvent(new GenericEvent(GenericEvent.CHANGE, evt.data.pvo));
		}
		private function onMainImageLoaded(Evt:Event):void {
			dispatchEvent(new GenericEvent(GenericEvent.STARTED));
		}
		public function set product(vo:ProductVO):void {
			_pvo = vo;
		}
		public function get product():ProductVO {
			return _pvo;
		}
		public function get bitmap():BitmapData {
			if (_currentBitmap) {
				_currentBitmap.dispose();
				_currentBitmap = null;
			}
			_currentBitmap = new BitmapData(_baseW, _baseH, false, 0xffffff);
			_currentBitmap.draw(this);
			return _currentBitmap;
		}
		public function set collection(fc:FlipCollection):void {
			_collection = fc;
		}
		public function get collection():FlipCollection {
			return _collection;
		}
		public function set thumbWidth(n:Number):void {
			_thumbW = n;
		}
		public function get thumbWidth():Number {
			return _thumbW;
		}
		public function set thumbHeight(n:Number):void {
			
			_thumbH = n;
		}
		public function get thumbHeight():Number {
			return _thumbH;
		}
		public function loadThumbnails():void {
			_imagecopy.loadThumbnails();
		}
		public function destroy():void {
			_bg.graphics.clear();
			removeChild(_bg);
			parent.removeChild(this);
			_pvo = null;
			_collection = null;
			removeEventListener(Event.ADDED_TO_STAGE, initializeComponent);
			if (_currentBitmap) {
				_currentBitmap.dispose();
				_currentBitmap = null;
			}
		}
		
	}
	
}